home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / Direct3D / RTPatch / rtpatch.cpp < prev    next >
C/C++ Source or Header  |  2001-10-08  |  33KB  |  669 lines

  1. //-----------------------------------------------------------------------------
  2. // File: RTPatch.cpp
  3. //
  4. // Desc: Example code showing how to use patches in D3D.
  5. //
  6. // Copyright (c) 1995-2001 Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #define STRICT
  9. #include <tchar.h>
  10. #include <math.h>
  11. #include <stdio.h>
  12. #include <D3DX8.h>
  13. #include "D3DApp.h"
  14. #include "D3DFile.h"
  15. #include "D3DFont.h"
  16. #include "D3DUtil.h"
  17. #include "DXUtil.h"
  18. #include "resource.h"
  19.  
  20.  
  21.  
  22. //-----------------------------------------------------------------------------
  23. // Defines, constants, and global variables
  24. //-----------------------------------------------------------------------------
  25. // Utah Teapot Bezier Patch Data
  26. const float teapotData[] = 
  27. {
  28.     // xBody_Back
  29.     -80.00f,  0.00f, 30.00f,     -80.00f,-44.80f, 30.00f,     -44.80f,-80.00f, 30.00f,       0.00f,-80.00f, 30.00f,
  30.     -80.00f,  0.00f, 12.00f,     -80.00f,-44.80f, 12.00f,     -44.80f,-80.00f, 12.00f,       0.00f,-80.00f, 12.00f,
  31.     -60.00f,  0.00f,  3.00f,     -60.00f,-33.60f,  3.00f,     -33.60f,-60.00f,  3.00f,       0.00f,-60.00f,  3.00f,
  32.     -60.00f,  0.00f,  0.00f,     -60.00f,-33.60f,  0.00f,     -33.60f,-60.00f,  0.00f,       0.00f,-60.00f,  0.00f,
  33.  
  34.       0.00f,-80.00f, 30.00f,      44.80f,-80.00f, 30.00f,      80.00f,-44.80f, 30.00f,      80.00f,  0.00f, 30.00f,
  35.       0.00f,-80.00f, 12.00f,      44.80f,-80.00f, 12.00f,      80.00f,-44.80f, 12.00f,      80.00f,  0.00f, 12.00f,
  36.       0.00f,-60.00f,  3.00f,      33.60f,-60.00f,  3.00f,      60.00f,-33.60f,  3.00f,      60.00f,  0.00f,  3.00f,
  37.       0.00f,-60.00f,  0.00f,      33.60f,-60.00f,  0.00f,      60.00f,-33.60f,  0.00f,      60.00f,  0.00f,  0.00f,
  38.  
  39.     -60.00f,  0.00f, 90.00f,     -60.00f,-33.60f, 90.00f,     -33.60f,-60.00f, 90.00f,       0.00f,-60.00f, 90.00f,
  40.     -70.00f,  0.00f, 69.00f,     -70.00f,-39.20f, 69.00f,     -39.20f,-70.00f, 69.00f,       0.00f,-70.00f, 69.00f,
  41.     -80.00f,  0.00f, 48.00f,     -80.00f,-44.80f, 48.00f,     -44.80f,-80.00f, 48.00f,       0.00f,-80.00f, 48.00f,
  42.     -80.00f,  0.00f, 30.00f,     -80.00f,-44.80f, 30.00f,     -44.80f,-80.00f, 30.00f,       0.00f,-80.00f, 30.00f,
  43.  
  44.       0.00f,-60.00f, 90.00f,      33.60f,-60.00f, 90.00f,      60.00f,-33.60f, 90.00f,      60.00f,  0.00f, 90.00f,
  45.       0.00f,-70.00f, 69.00f,      39.20f,-70.00f, 69.00f,      70.00f,-39.20f, 69.00f,      70.00f,  0.00f, 69.00f,
  46.       0.00f,-80.00f, 48.00f,      44.80f,-80.00f, 48.00f,      80.00f,-44.80f, 48.00f,      80.00f,  0.00f, 48.00f,
  47.       0.00f,-80.00f, 30.00f,      44.80f,-80.00f, 30.00f,      80.00f,-44.80f, 30.00f,      80.00f,  0.00f, 30.00f,
  48.  
  49.     -56.00f,  0.00f, 90.00f,     -56.00f,-31.36f, 90.00f,     -31.36f,-56.00f, 90.00f,       0.00f,-56.00f, 90.00f,
  50.     -53.50f,  0.00f, 95.25f,     -53.50f,-29.96f, 95.25f,     -29.96f,-53.50f, 95.25f,       0.00f,-53.50f, 95.25f,
  51.     -57.50f,  0.00f, 95.25f,     -57.50f,-32.20f, 95.25f,     -32.20f,-57.50f, 95.25f,       0.00f,-57.50f, 95.25f,
  52.     -60.00f,  0.00f, 90.00f,     -60.00f,-33.60f, 90.00f,     -33.60f,-60.00f, 90.00f,       0.00f,-60.00f, 90.00f,
  53.  
  54.       0.00f,-56.00f, 90.00f,      31.36f,-56.00f, 90.00f,      56.00f,-31.36f, 90.00f,      56.00f,  0.00f, 90.00f,
  55.       0.00f,-53.50f, 95.25f,      29.96f,-53.50f, 95.25f,      53.50f,-29.96f, 95.25f,      53.50f,  0.00f, 95.25f,
  56.       0.00f,-57.50f, 95.25f,      32.20f,-57.50f, 95.25f,      57.50f,-32.20f, 95.25f,      57.50f,  0.00f, 95.25f,
  57.       0.00f,-60.00f, 90.00f,      33.60f,-60.00f, 90.00f,      60.00f,-33.60f, 90.00f,      60.00f,  0.00f, 90.00f,
  58.  
  59.       // xBody_Front
  60.      80.00f,  0.00f, 30.00f,      80.00f, 44.80f, 30.00f,      44.80f, 80.00f, 30.00f,       0.00f, 80.00f, 30.00f,
  61.      80.00f,  0.00f, 12.00f,      80.00f, 44.80f, 12.00f,      44.80f, 80.00f, 12.00f,       0.00f, 80.00f, 12.00f,
  62.      60.00f,  0.00f,  3.00f,      60.00f, 33.60f,  3.00f,      33.60f, 60.00f,  3.00f,       0.00f, 60.00f,  3.00f,
  63.      60.00f,  0.00f,  0.00f,      60.00f, 33.60f,  0.00f,      33.60f, 60.00f,  0.00f,       0.00f, 60.00f,  0.00f,
  64.  
  65.       0.00f, 80.00f, 30.00f,     -44.80f, 80.00f, 30.00f,     -80.00f, 44.80f, 30.00f,     -80.00f,  0.00f, 30.00f,
  66.       0.00f, 80.00f, 12.00f,     -44.80f, 80.00f, 12.00f,     -80.00f, 44.80f, 12.00f,     -80.00f,  0.00f, 12.00f,
  67.       0.00f, 60.00f,  3.00f,     -33.60f, 60.00f,  3.00f,     -60.00f, 33.60f,  3.00f,     -60.00f,  0.00f,  3.00f,
  68.       0.00f, 60.00f,  0.00f,     -33.60f, 60.00f,  0.00f,     -60.00f, 33.60f,  0.00f,     -60.00f,  0.00f,  0.00f,
  69.  
  70.      60.00f,  0.00f, 90.00f,      60.00f, 33.60f, 90.00f,      33.60f, 60.00f, 90.00f,       0.00f, 60.00f, 90.00f,
  71.      70.00f,  0.00f, 69.00f,      70.00f, 39.20f, 69.00f,      39.20f, 70.00f, 69.00f,       0.00f, 70.00f, 69.00f,
  72.      80.00f,  0.00f, 48.00f,      80.00f, 44.80f, 48.00f,      44.80f, 80.00f, 48.00f,       0.00f, 80.00f, 48.00f,
  73.      80.00f,  0.00f, 30.00f,      80.00f, 44.80f, 30.00f,      44.80f, 80.00f, 30.00f,       0.00f, 80.00f, 30.00f,
  74.  
  75.       0.00f, 60.00f, 90.00f,     -33.60f, 60.00f, 90.00f,     -60.00f, 33.60f, 90.00f,     -60.00f,  0.00f, 90.00f,
  76.       0.00f, 70.00f, 69.00f,     -39.20f, 70.00f, 69.00f,     -70.00f, 39.20f, 69.00f,     -70.00f,  0.00f, 69.00f,
  77.       0.00f, 80.00f, 48.00f,     -44.80f, 80.00f, 48.00f,     -80.00f, 44.80f, 48.00f,     -80.00f,  0.00f, 48.00f,
  78.       0.00f, 80.00f, 30.00f,     -44.80f, 80.00f, 30.00f,     -80.00f, 44.80f, 30.00f,     -80.00f,  0.00f, 30.00f,
  79.  
  80.      56.00f,  0.00f, 90.00f,      56.00f, 31.36f, 90.00f,      31.36f, 56.00f, 90.00f,       0.00f, 56.00f, 90.00f,
  81.      53.50f,  0.00f, 95.25f,      53.50f, 29.96f, 95.25f,      29.96f, 53.50f, 95.25f,       0.00f, 53.50f, 95.25f,
  82.      57.50f,  0.00f, 95.25f,      57.50f, 32.20f, 95.25f,      32.20f, 57.50f, 95.25f,       0.00f, 57.50f, 95.25f,
  83.      60.00f,  0.00f, 90.00f,      60.00f, 33.60f, 90.00f,      33.60f, 60.00f, 90.00f,       0.00f, 60.00f, 90.00f,
  84.  
  85.       0.00f, 56.00f, 90.00f,     -31.36f, 56.00f, 90.00f,     -56.00f, 31.36f, 90.00f,     -56.00f,  0.00f, 90.00f,
  86.       0.00f, 53.50f, 95.25f,     -29.96f, 53.50f, 95.25f,     -53.50f, 29.96f, 95.25f,     -53.50f,  0.00f, 95.25f,
  87.       0.00f, 57.50f, 95.25f,     -32.20f, 57.50f, 95.25f,     -57.50f, 32.20f, 95.25f,     -57.50f,  0.00f, 95.25f,
  88.       0.00f, 60.00f, 90.00f,     -33.60f, 60.00f, 90.00f,     -60.00f, 33.60f, 90.00f,     -60.00f,  0.00f, 90.00f,
  89.  
  90.     // Handle
  91.     -64.00f,  0.00f, 75.00f,     -64.00f, 12.00f, 75.00f,     -60.00f, 12.00f, 84.00f,     -60.00f,  0.00f, 84.00f,
  92.     -92.00f,  0.00f, 75.00f,     -92.00f, 12.00f, 75.00f,    -100.00f, 12.00f, 84.00f,    -100.00f,  0.00f, 84.00f,
  93.    -108.00f,  0.00f, 75.00f,    -108.00f, 12.00f, 75.00f,    -120.00f, 12.00f, 84.00f,    -120.00f,  0.00f, 84.00f,
  94.    -108.00f,  0.00f, 66.00f,    -108.00f, 12.00f, 66.00f,    -120.00f, 12.00f, 66.00f,    -120.00f,  0.00f, 66.00f,
  95.  
  96.     -60.00f,  0.00f, 84.00f,     -60.00f,-12.00f, 84.00f,     -64.00f,-12.00f, 75.00f,     -64.00f,  0.00f, 75.00f,
  97.    -100.00f,  0.00f, 84.00f,    -100.00f,-12.00f, 84.00f,     -92.00f,-12.00f, 75.00f,     -92.00f,  0.00f, 75.00f,
  98.    -120.00f,  0.00f, 84.00f,    -120.00f,-12.00f, 84.00f,    -108.00f,-12.00f, 75.00f,    -108.00f,  0.00f, 75.00f,
  99.    -120.00f,  0.00f, 66.00f,    -120.00f,-12.00f, 66.00f,    -108.00f,-12.00f, 66.00f,    -108.00f,  0.00f, 66.00f,
  100.  
  101.    -108.00f,  0.00f, 66.00f,    -108.00f, 12.00f, 66.00f,    -120.00f, 12.00f, 66.00f,    -120.00f,  0.00f, 66.00f,
  102.    -108.00f,  0.00f, 57.00f,    -108.00f, 12.00f, 57.00f,    -120.00f, 12.00f, 48.00f,    -120.00f,  0.00f, 48.00f,
  103.    -100.00f,  0.00f, 39.00f,    -100.00f, 12.00f, 39.00f,    -106.00f, 12.00f, 31.50f,    -106.00f,  0.00f, 31.50f,
  104.     -80.00f,  0.00f, 30.00f,     -80.00f, 12.00f, 30.00f,     -76.00f, 12.00f, 18.00f,     -76.00f,  0.00f, 18.00f,
  105.  
  106.    -120.00f,  0.00f, 66.00f,    -120.00f,-12.00f, 66.00f,    -108.00f,-12.00f, 66.00f,    -108.00f,  0.00f, 66.00f,
  107.    -120.00f,  0.00f, 48.00f,    -120.00f,-12.00f, 48.00f,    -108.00f,-12.00f, 57.00f,    -108.00f,  0.00f, 57.00f,
  108.    -106.00f,  0.00f, 31.50f,    -106.00f,-12.00f, 31.50f,    -100.00f,-12.00f, 39.00f,    -100.00f,  0.00f, 39.00f,
  109.     -76.00f,  0.00f, 18.00f,     -76.00f,-12.00f, 18.00f,     -80.00f,-12.00f, 30.00f,     -80.00f,  0.00f, 30.00f,
  110.  
  111.     // Spout
  112.      68.00f,  0.00f, 51.00f,      68.00f, 26.40f, 51.00f,      68.00f, 26.40f, 18.00f,      68.00f,  0.00f, 18.00f,
  113.     104.00f,  0.00f, 51.00f,     104.00f, 26.40f, 51.00f,     124.00f, 26.40f, 27.00f,     124.00f,  0.00f, 27.00f,
  114.      92.00f,  0.00f, 78.00f,      92.00f, 10.00f, 78.00f,      96.00f, 10.00f, 75.00f,      96.00f,  0.00f, 75.00f,
  115.     108.00f,  0.00f, 90.00f,     108.00f, 10.00f, 90.00f,     132.00f, 10.00f, 90.00f,     132.00f,  0.00f, 90.00f,
  116.  
  117.      68.00f,  0.00f, 18.00f,      68.00f,-26.40f, 18.00f,      68.00f,-26.40f, 51.00f,      68.00f,  0.00f, 51.00f,
  118.     124.00f,  0.00f, 27.00f,     124.00f,-26.40f, 27.00f,     104.00f,-26.40f, 51.00f,     104.00f,  0.00f, 51.00f,
  119.      96.00f,  0.00f, 75.00f,      96.00f,-10.00f, 75.00f,      92.00f,-10.00f, 78.00f,      92.00f,  0.00f, 78.00f,
  120.     132.00f,  0.00f, 90.00f,     132.00f,-10.00f, 90.00f,     108.00f,-10.00f, 90.00f,     108.00f,  0.00f, 90.00f,
  121.  
  122.     108.00f,  0.00f, 90.00f,     108.00f, 10.00f, 90.00f,     132.00f, 10.00f, 90.00f,     132.00f,  0.00f, 90.00f,
  123.     112.00f,  0.00f, 93.00f,     112.00f, 10.00f, 93.00f,     141.00f, 10.00f, 93.75f,     141.00f,  0.00f, 93.75f,
  124.     116.00f,  0.00f, 93.00f,     116.00f,  6.00f, 93.00f,     138.00f,  6.00f, 94.50f,     138.00f,  0.00f, 94.50f,
  125.     112.00f,  0.00f, 90.00f,     112.00f,  6.00f, 90.00f,     128.00f,  6.00f, 90.00f,     128.00f,  0.00f, 90.00f,
  126.  
  127.     132.00f,  0.00f, 90.00f,     132.00f,-10.00f, 90.00f,     108.00f,-10.00f, 90.00f,     108.00f,  0.00f, 90.00f,
  128.     141.00f,  0.00f, 93.75f,     141.00f,-10.00f, 93.75f,     112.00f,-10.00f, 93.00f,     112.00f,  0.00f, 93.00f,
  129.     138.00f,  0.00f, 94.50f,     138.00f, -6.00f, 94.50f,     116.00f, -6.00f, 93.00f,     116.00f,  0.00f, 93.00f,
  130.     128.00f,  0.00f, 90.00f,     128.00f, -6.00f, 90.00f,     112.00f, -6.00f, 90.00f,     112.00f,  0.00f, 90.00f,
  131.  
  132.     // Lip
  133.      50.00f,  0.00f, 90.00f,      50.00f, 28.00f, 90.00f,      28.00f, 50.00f, 90.00f,       0.00f, 50.00f, 90.00f,
  134.      52.00f,  0.00f, 90.00f,      52.00f, 29.12f, 90.00f,      29.12f, 52.00f, 90.00f,       0.00f, 52.00f, 90.00f,
  135.      54.00f,  0.00f, 90.00f,      54.00f, 30.24f, 90.00f,      30.24f, 54.00f, 90.00f,       0.00f, 54.00f, 90.00f,
  136.      56.00f,  0.00f, 90.00f,      56.00f, 31.36f, 90.00f,      31.36f, 56.00f, 90.00f,       0.00f, 56.00f, 90.00f,
  137.  
  138.       0.00f, 50.00f, 90.00f,     -28.00f, 50.00f, 90.00f,     -50.00f, 28.00f, 90.00f,     -50.00f,  0.00f, 90.00f,
  139.       0.00f, 52.00f, 90.00f,     -29.12f, 52.00f, 90.00f,     -52.00f, 29.12f, 90.00f,     -52.00f,  0.00f, 90.00f,
  140.       0.00f, 54.00f, 90.00f,     -30.24f, 54.00f, 90.00f,     -54.00f, 30.24f, 90.00f,     -54.00f,  0.00f, 90.00f,
  141.       0.00f, 56.00f, 90.00f,     -31.36f, 56.00f, 90.00f,     -56.00f, 31.36f, 90.00f,     -56.00f,  0.00f, 90.00f,
  142.  
  143.     -50.00f,  0.00f, 90.00f,     -50.00f,-28.00f, 90.00f,     -28.00f,-50.00f, 90.00f,       0.00f,-50.00f, 90.00f,
  144.     -52.00f,  0.00f, 90.00f,     -52.00f,-29.12f, 90.00f,     -29.12f,-52.00f, 90.00f,       0.00f,-52.00f, 90.00f,
  145.     -54.00f,  0.00f, 90.00f,     -54.00f,-30.24f, 90.00f,     -30.24f,-54.00f, 90.00f,       0.00f,-54.00f, 90.00f,
  146.     -56.00f,  0.00f, 90.00f,     -56.00f,-31.36f, 90.00f,     -31.36f,-56.00f, 90.00f,       0.00f,-56.00f, 90.00f,
  147.  
  148.       0.00f,-50.00f, 90.00f,      28.00f,-50.00f, 90.00f,      50.00f,-28.00f, 90.00f,      50.00f,  0.00f, 90.00f,
  149.       0.00f,-52.00f, 90.00f,      29.12f,-52.00f, 90.00f,      52.00f,-29.12f, 90.00f,      52.00f,  0.00f, 90.00f,
  150.       0.00f,-54.00f, 90.00f,      30.24f,-54.00f, 90.00f,      54.00f,-30.24f, 90.00f,      54.00f,  0.00f, 90.00f,
  151.       0.00f,-56.00f, 90.00f,      31.36f,-56.00f, 90.00f,      56.00f,-31.36f, 90.00f,      56.00f,  0.00f, 90.00f,
  152.  
  153.     // Lid
  154.       8.00f,  0.00f,102.00f,       8.00f,  4.48f,102.00f,       4.48f,  8.00f,102.00f,       0.00f,  8.00f,102.00f,
  155.      16.00f,  0.00f, 96.00f,      16.00f,  8.96f, 96.00f,       8.96f, 16.00f, 96.00f,       0.00f, 16.00f, 96.00f,
  156.      52.00f,  0.00f, 96.00f,      52.00f, 29.12f, 96.00f,      29.12f, 52.00f, 96.00f,       0.00f, 52.00f, 96.00f,
  157.      52.00f,  0.00f, 90.00f,      52.00f, 29.12f, 90.00f,      29.12f, 52.00f, 90.00f,       0.00f, 52.00f, 90.00f,
  158.  
  159.       0.00f,  8.00f,102.00f,      -4.48f,  8.00f,102.00f,      -8.00f,  4.48f,102.00f,      -8.00f,  0.00f,102.00f,
  160.       0.00f, 16.00f, 96.00f,      -8.96f, 16.00f, 96.00f,     -16.00f,  8.96f, 96.00f,     -16.00f,  0.00f, 96.00f,
  161.       0.00f, 52.00f, 96.00f,     -29.12f, 52.00f, 96.00f,     -52.00f, 29.12f, 96.00f,     -52.00f,  0.00f, 96.00f,
  162.       0.00f, 52.00f, 90.00f,     -29.12f, 52.00f, 90.00f,     -52.00f, 29.12f, 90.00f,     -52.00f,  0.00f, 90.00f,
  163.  
  164.      -8.00f,  0.00f,102.00f,      -8.00f, -4.48f,102.00f,      -4.48f, -8.00f,102.00f,       0.00f, -8.00f,102.00f,
  165.     -16.00f,  0.00f, 96.00f,     -16.00f, -8.96f, 96.00f,      -8.96f,-16.00f, 96.00f,       0.00f,-16.00f, 96.00f,
  166.     -52.00f,  0.00f, 96.00f,     -52.00f,-29.12f, 96.00f,     -29.12f,-52.00f, 96.00f,       0.00f,-52.00f, 96.00f,
  167.     -52.00f,  0.00f, 90.00f,     -52.00f,-29.12f, 90.00f,     -29.12f,-52.00f, 90.00f,       0.00f,-52.00f, 90.00f,
  168.  
  169.       0.00f, -8.00f,102.00f,       4.48f, -8.00f,102.00f,       8.00f, -4.48f,102.00f,       8.00f,  0.00f,102.00f,
  170.       0.00f,-16.00f, 96.00f,       8.96f,-16.00f, 96.00f,      16.00f, -8.96f, 96.00f,      16.00f,  0.00f, 96.00f,
  171.       0.00f,-52.00f, 96.00f,      29.12f,-52.00f, 96.00f,      52.00f,-29.12f, 96.00f,      52.00f,  0.00f, 96.00f,
  172.       0.00f,-52.00f, 90.00f,      29.12f,-52.00f, 90.00f,      52.00f,-29.12f, 90.00f,      52.00f,  0.00f, 90.00f,
  173.  
  174.     // Knob
  175.       0.00f,  0.00f,120.00f,       0.00f,  0.00f,120.00f,       0.00f,  0.00f,120.00f,       0.00f,  0.00f,120.00f,
  176.      32.00f,  0.00f,120.00f,      32.00f, 18.00f,120.00f,      18.00f, 32.00f,120.00f,       0.00f, 32.00f,120.00f,
  177.       0.00f,  0.00f,108.00f,       0.00f,  0.00f,108.00f,       0.00f,  0.00f,108.00f,       0.00f,  0.00f,108.00f,
  178.       8.00f,  0.00f,102.00f,       8.00f,  4.48f,102.00f,       4.48f,  8.00f,102.00f,       0.00f,  8.00f,102.00f,
  179.  
  180.       0.00f,  0.00f,120.00f,       0.00f,  0.00f,120.00f,       0.00f,  0.00f,120.00f,       0.00f,  0.00f,120.00f,
  181.       0.00f, 32.00f,120.00f,     -18.00f, 32.00f,120.00f,     -32.00f, 18.00f,120.00f,     -32.00f,  0.00f,120.00f,
  182.       0.00f,  0.00f,108.00f,       0.00f,  0.00f,108.00f,       0.00f,  0.00f,108.00f,       0.00f,  0.00f,108.00f,
  183.       0.00f,  8.00f,102.00f,      -4.48f,  8.00f,102.00f,      -8.00f,  4.48f,102.00f,      -8.00f,  0.00f,102.00f,
  184.  
  185.       0.00f,  0.00f,120.00f,       0.00f,  0.00f,120.00f,       0.00f,  0.00f,120.00f,       0.00f,  0.00f,120.00f,
  186.     -32.00f,  0.00f,120.00f,     -32.00f,-18.00f,120.00f,     -18.00f,-32.00f,120.00f,       0.00f,-32.00f,120.00f,
  187.       0.00f,  0.00f,108.00f,       0.00f,  0.00f,108.00f,       0.00f,  0.00f,108.00f,       0.00f,  0.00f,108.00f,
  188.      -8.00f,  0.00f,102.00f,      -8.00f, -4.48f,102.00f,      -4.48f, -8.00f,102.00f,       0.00f, -8.00f,102.00f,
  189.  
  190.       0.00f,  0.00f,120.00f,       0.00f,  0.00f,120.00f,       0.00f,  0.00f,120.00f,       0.00f,  0.00f,120.00f,
  191.       0.00f,-32.00f,120.00f,      18.00f,-32.00f,120.00f,      32.00f,-18.00f,120.00f,      32.00f,  0.00f,120.00f,
  192.       0.00f,  0.00f,108.00f,       0.00f,  0.00f,108.00f,       0.00f,  0.00f,108.00f,       0.00f,  0.00f,108.00f,
  193.       0.00f, -8.00f,102.00f,       4.48f, -8.00f,102.00f,       8.00f, -4.48f,102.00f,       8.00f,  0.00f,102.00f,
  194.  
  195.       // Bottom. Not part of original data set.
  196.       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,
  197.       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,
  198.       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,
  199.       0.00f, 60.00f,  0.00f,      33.60f, 60.00f,  0.00f,      60.00f, 33.60f,  0.00f,      60.00f,  0.00f,  0.00f, 
  200.  
  201.       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,
  202.       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,
  203.       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,
  204.       0.00f,-60.00f,  0.00f,     -33.60f,-60.00f,  0.00f,     -60.00f,-33.60f,  0.00f,     -60.00f,  0.00f,  0.00f, 
  205.  
  206.       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,
  207.       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,
  208.       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,
  209.      60.00f,  0.00f,  0.00f,      60.00f,-33.60f,  0.00f,      33.60f,-60.00f,  0.00f,       0.00f,-60.00f,  0.00f, 
  210.  
  211.       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,
  212.       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,
  213.       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,       0.00f,  0.00f,  0.00f,
  214.     -60.00f,  0.00f,  0.00f,     -60.00f, 33.60f,  0.00f,     -33.60f, 60.00f,  0.00f,       0.00f, 60.00f,  0.00f, 
  215.  
  216. };
  217.  
  218. inline float TeapotData(UINT patch, UINT u, UINT v, UINT coordIndex) 
  219. {
  220.     return teapotData[patch * 16 * 3 + v * 4 * 3 + u * 3 + coordIndex];
  221. }
  222.  
  223.  
  224. // A structure for our custom vertex type
  225. struct CUSTOMVERTEX
  226. {
  227.     FLOAT x, y, z; // The original position of the vertex
  228. };
  229.  
  230. // Our custom FVF, which describes our custom vertex structure
  231. #define D3DFVF_CUSTOMVERTEX ( D3DFVF_XYZ )
  232.  
  233. // Surface handles have to be > 0, but can be assigned arbitrarily.
  234. const UINT SURFACEHANDLEBASE = 1;
  235.  
  236.  
  237.  
  238.  
  239. //-----------------------------------------------------------------------------
  240. // Name: class CMyD3DApplication
  241. // Desc: Application class. The base class (CD3DApplication) provides the 
  242. //       generic functionality needed in all Direct3D samples. CMyD3DApplication 
  243. //       adds functionality specific to this sample program.
  244. //-----------------------------------------------------------------------------
  245. class CMyD3DApplication : public CD3DApplication
  246. {
  247.     CD3DFont*               m_pFont;          // Font for drawing text
  248.     LPDIRECT3DVERTEXBUFFER8 m_pVB;            // Buffer to hold vertices
  249.     DWORD                   m_hVShader;       // Handle to vertex shader
  250.     CD3DArcBall             m_ArcBall;        // Mouse rotation utility
  251.  
  252.     UINT                    m_numSegments;      // Amount to tessellate
  253.     BOOL                    m_bRetessellate;  // Need to retessellate patches
  254.     UINT                    m_numPatches;
  255.     BOOL                    m_bShowHelp;
  256.     BOOL                    m_bWireframe;
  257.  
  258.     HRESULT ConfirmDevice( D3DCAPS8*, DWORD, D3DFORMAT );
  259.  
  260. protected:
  261.     HRESULT OneTimeSceneInit();
  262.     HRESULT InitDeviceObjects();
  263.     HRESULT RestoreDeviceObjects();
  264.     HRESULT InvalidateDeviceObjects();
  265.     HRESULT DeleteDeviceObjects();
  266.     HRESULT Render();
  267.     HRESULT FrameMove();
  268.     HRESULT FinalCleanup();
  269.  
  270. public:
  271.     LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  272.     CMyD3DApplication();
  273. };
  274.  
  275.  
  276.  
  277.  
  278. //-----------------------------------------------------------------------------
  279. // Name: WinMain()
  280. // Desc: Entry point to the program. Initializes everything, and goes into a
  281. //       message-processing loop. Idle time is used to render the scene.
  282. //-----------------------------------------------------------------------------
  283. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  284. {
  285.     CMyD3DApplication d3dApp;
  286.  
  287.     if( FAILED( d3dApp.Create( hInst ) ) )
  288.         return 0;
  289.     return d3dApp.Run();
  290. }
  291.  
  292.  
  293.  
  294.  
  295. //-----------------------------------------------------------------------------
  296. // Name: CMyD3DApplication()
  297. // Desc: Application constructor. Sets attributes for the app.
  298. //-----------------------------------------------------------------------------
  299. CMyD3DApplication::CMyD3DApplication()
  300. {
  301.     m_strWindowTitle    = _T("RTPatch: D3D Patch Example");
  302.     m_bUseDepthBuffer   = TRUE;
  303.     m_bShowCursorWhenFullscreen = TRUE;
  304.  
  305.     m_pFont             = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  306.     m_pVB               = NULL;
  307.     m_hVShader          = 0;
  308.     m_bRetessellate     = TRUE;
  309.     m_numSegments       = 4;
  310.     m_bShowHelp         = FALSE;
  311.     m_bWireframe        = FALSE;
  312. }
  313.  
  314.  
  315.  
  316.  
  317. //-----------------------------------------------------------------------------
  318. // Name: OneTimeSceneInit()
  319. // Desc: Called during initial app startup, this function performs all the
  320. //       permanent initialization.
  321. //-----------------------------------------------------------------------------
  322. HRESULT CMyD3DApplication::OneTimeSceneInit()
  323. {
  324.     // Set cursor to indicate that user can move the object with the mouse
  325. #ifdef _WIN64
  326.     SetClassLongPtr( m_hWnd, GCLP_HCURSOR, (LONG_PTR)LoadCursor( NULL, IDC_SIZEALL ) );
  327. #else
  328.     SetClassLong( m_hWnd, GCL_HCURSOR, (LONG)LoadCursor( NULL, IDC_SIZEALL ) );
  329. #endif
  330.     return S_OK;
  331. }
  332.  
  333.  
  334.  
  335.  
  336. //-----------------------------------------------------------------------------
  337. // Name: FrameMove()
  338. // Desc: Called once per frame, the call is the entry point for animating
  339. //       the scene.
  340. //-----------------------------------------------------------------------------
  341. HRESULT CMyD3DApplication::FrameMove()
  342. {
  343.     // Set up viewing postion from ArcBall
  344.     m_pd3dDevice->SetTransform( D3DTS_WORLD, m_ArcBall.GetRotationMatrix() );
  345.  
  346.     return S_OK;
  347. }
  348.  
  349.  
  350.  
  351.  
  352. //-----------------------------------------------------------------------------
  353. // Name: Render()
  354. // Desc: 
  355. //-----------------------------------------------------------------------------
  356. HRESULT CMyD3DApplication::Render()
  357. {
  358.     // Clear the viewport
  359.     m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 
  360.                          0x000000ff, 1.0f, 0L );
  361.  
  362.     if( m_bWireframe )
  363.         m_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME );
  364.     else
  365.         m_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
  366.  
  367.     // Begin the scene
  368.     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
  369.     {
  370.         m_pd3dDevice->SetVertexShader( m_hVShader );
  371.         m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
  372.  
  373.         for( UINT i = 0; i < m_numPatches; i++ ) 
  374.         {
  375.             float numSegs[4];
  376.             numSegs[0] = (FLOAT)m_numSegments;
  377.             numSegs[1] = (FLOAT)m_numSegments;
  378.             numSegs[2] = (FLOAT)m_numSegments;
  379.             numSegs[3] = (FLOAT)m_numSegments;
  380.             if ( m_bRetessellate ) 
  381.             {
  382.                 // First time through, tesellate
  383.                 D3DRECTPATCH_INFO info;
  384.                 info.StartVertexOffsetWidth = 0;
  385.                 info.StartVertexOffsetHeight = i * 4;
  386.                 info.Width = 4;
  387.                 info.Height = 4;
  388.                 info.Stride = 4; // verticies to next row of verticies
  389.                 info.Basis = D3DBASIS_BEZIER;
  390.                 info.Order = D3DORDER_CUBIC;
  391.                 m_pd3dDevice->DrawRectPatch(SURFACEHANDLEBASE + i, numSegs, &info);
  392.             }
  393.             else 
  394.             {
  395.                 // After the first time, use the existing surface handles
  396.                 m_pd3dDevice->DrawRectPatch(SURFACEHANDLEBASE + i, numSegs, 0);
  397.             }
  398.         }
  399.  
  400.         m_bRetessellate = FALSE;
  401.         
  402.         // Output statistics
  403.         m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
  404.         m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
  405.         TCHAR szSegments[150];
  406.         wsprintf(szSegments, TEXT("%d segments per patch"), m_numSegments);
  407.         m_pFont->DrawText( 2, 40, D3DCOLOR_ARGB(255,255,255,255), szSegments );
  408.  
  409.         if( m_bShowHelp )
  410.         {
  411.             m_pFont->DrawText( 2, 60, D3DCOLOR_ARGB(255,255,255,255), 
  412.                 TEXT("Up/Down arrows: change number of segments") );
  413.             m_pFont->DrawText( 2, 80, D3DCOLOR_ARGB(255,255,255,255), 
  414.                 TEXT("W: toggle wireframe") );
  415.         }
  416.         else
  417.         {
  418.             m_pFont->DrawText( 2, 60, D3DCOLOR_ARGB(255,255,255,255), 
  419.                 TEXT("Press F1 for Help") );
  420.         }
  421.  
  422.         // End the scene.
  423.         m_pd3dDevice->EndScene();
  424.     }
  425.  
  426.     return S_OK;
  427. }
  428.  
  429.  
  430.  
  431.  
  432. //-----------------------------------------------------------------------------
  433. // Name: InitDeviceObjects()
  434. // Desc: This creates all device-dependent managed objects, such as managed
  435. //       textures and managed vertex buffers.
  436. //-----------------------------------------------------------------------------
  437. HRESULT CMyD3DApplication::InitDeviceObjects()
  438. {
  439.     // Initialize the font's internal textures
  440.     m_pFont->InitDeviceObjects( m_pd3dDevice );
  441.  
  442.     // Create a vertex buffer and fill it with our vertices.
  443.     UINT numVerts = sizeof(teapotData) / (3 * sizeof(float));
  444.     m_numPatches = numVerts / (4 * 4);
  445.  
  446.     if( FAILED( m_pd3dDevice->CreateVertexBuffer( numVerts*sizeof(CUSTOMVERTEX),
  447.         D3DUSAGE_RTPATCHES, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &m_pVB ) ) )
  448.     {
  449.         return E_FAIL;
  450.     }
  451.  
  452.     VOID* pVertices;
  453.     if( FAILED( m_pVB->Lock( 0, numVerts*sizeof(CUSTOMVERTEX), (BYTE**)&pVertices, 0 ) ) )
  454.         return E_FAIL;
  455.     CUSTOMVERTEX* pVBase = (CUSTOMVERTEX*) pVertices;
  456.     for( UINT patch = 0; patch < m_numPatches; patch++ ) 
  457.     {
  458.         for( UINT v = 0; v < 4; v++ ) 
  459.         {
  460.             for ( UINT u = 0; u < 4; u++ ) 
  461.             {
  462.                 CUSTOMVERTEX* pV = pVBase + (patch * 16 + u + v * 4);
  463.                 pV->x = TeapotData(patch, u, v, 0);
  464.                 pV->y = TeapotData(patch, u, v, 1);
  465.                 pV->z = -TeapotData(patch, u, v, 2);    // Convert handedness of coordinate system
  466.             }
  467.         }
  468.     }
  469.     m_pVB->Unlock();
  470.  
  471.     return S_OK;
  472. }
  473.  
  474.  
  475.  
  476.  
  477. //-----------------------------------------------------------------------------
  478. // Name: RestoreDeviceObjects()
  479. // Desc: Restore device-memory objects and state after a device is created or
  480. //       resized.
  481. //-----------------------------------------------------------------------------
  482. HRESULT CMyD3DApplication::RestoreDeviceObjects()
  483. {
  484.     HRESULT hr;
  485.  
  486.     m_pFont->RestoreDeviceObjects();
  487.  
  488.  
  489.     // Set up our view matrix. A view matrix can be defined given an eye point,
  490.     // a point to lookat, and a direction for which way is up. 
  491.     D3DXMATRIX matView;
  492.     D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3( 0.0f, 3.0f,-400.0f ), 
  493.                                   &D3DXVECTOR3( 0.0f, 0.0f, 0.0f ), 
  494.                                   &D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) );
  495.     m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
  496.  
  497.     // Set up world matrix
  498.     D3DXMATRIX matWorld;
  499.     D3DXMatrixIdentity( &matWorld );
  500.     m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  501.  
  502.     D3DXMATRIX matProj;
  503.     FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
  504.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 800.0f );
  505.     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  506.  
  507.     m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE );
  508.     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,      TRUE );
  509.     m_pd3dDevice->SetRenderState( D3DRS_LIGHTING,     TRUE );
  510.     m_pd3dDevice->SetRenderState( D3DRS_NORMALIZENORMALS, TRUE );
  511.  
  512.     m_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x00202020 );
  513.  
  514.     // Set up a material. The material here just has the diffuse and ambient
  515.     // colors set to white.
  516.     D3DMATERIAL8 mtrl;
  517.     ZeroMemory( &mtrl, sizeof(D3DMATERIAL8) );
  518.     mtrl.Diffuse.r = mtrl.Ambient.r = 1.0f;
  519.     mtrl.Diffuse.g = mtrl.Ambient.g = 1.0f;
  520.     mtrl.Diffuse.b = mtrl.Ambient.b = 1.0f;
  521.     mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f;
  522.     m_pd3dDevice->SetMaterial( &mtrl );
  523.  
  524.     // Set up a light
  525.     D3DLIGHT8 light;
  526.     light.Type         = D3DLIGHT_DIRECTIONAL;
  527.     light.Diffuse.r    = light.Diffuse.g  = light.Diffuse.b  = 1.0f;
  528.     light.Specular.r   = light.Specular.g = light.Specular.b = 0.0f;
  529.     light.Ambient.r    = light.Ambient.g  = light.Ambient.b  = 0.3f;
  530.     light.Position     = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
  531.     D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &D3DXVECTOR3( 0.0f, 1.0f, 1.0f ) );
  532.     light.Attenuation0 = light.Attenuation1 = light.Attenuation2 = 0.0f;
  533.     light.Range        = sqrtf(FLT_MAX);
  534.     m_pd3dDevice->SetLight(0, &light );
  535.     m_pd3dDevice->LightEnable(0, TRUE );
  536.  
  537.     m_ArcBall.SetWindow( m_d3dsdBackBuffer.Width, m_d3dsdBackBuffer.Height, 1.0f );
  538.     m_ArcBall.SetRadius( 1.0f );
  539.  
  540.     DWORD decl[] =
  541.     {
  542.        /* Vertex Data */
  543.         D3DVSD_STREAM(0),
  544.         D3DVSD_REG( D3DVSDE_POSITION,  D3DVSDT_FLOAT3),
  545.  
  546.         /* Data generation section implemented as a virtual stream */
  547.         D3DVSD_STREAM_TESS(),
  548.  
  549.         /* Generate normal _using_ the position input and copy it to the normal register (output) */
  550.         D3DVSD_TESSNORMAL( D3DVSDE_POSITION, D3DVSDE_NORMAL ),
  551.  
  552.         D3DVSD_END()
  553.     };
  554.  
  555.     hr = m_pd3dDevice->CreateVertexShader( decl, NULL, &m_hVShader, 0 );
  556.     if( FAILED( hr ) )
  557.         return hr;
  558.  
  559.     m_bRetessellate = TRUE;
  560.  
  561.     return S_OK;
  562. }
  563.  
  564.  
  565.  
  566.  
  567. //-----------------------------------------------------------------------------
  568. // Name: InvalidateDeviceObjects()
  569. // Desc: Called when the device-dependent objects are about to be lost.
  570. //-----------------------------------------------------------------------------
  571. HRESULT CMyD3DApplication::InvalidateDeviceObjects()
  572. {
  573.     m_pFont->InvalidateDeviceObjects();
  574.  
  575.     for( UINT i = 0; i < m_numPatches; i++ ) 
  576.         m_pd3dDevice->DeletePatch(SURFACEHANDLEBASE + i);
  577.     
  578.     if( m_hVShader != 0 )
  579.         m_pd3dDevice->DeleteVertexShader(m_hVShader);
  580.  
  581.     return S_OK;
  582. }
  583.  
  584.  
  585.  
  586.  
  587. //-----------------------------------------------------------------------------
  588. // Name: DeleteDeviceObjects()
  589. // Desc: Called when the app is exiting, or the device is being changed,
  590. //       this function deletes any device dependent objects.
  591. //-----------------------------------------------------------------------------
  592. HRESULT CMyD3DApplication::DeleteDeviceObjects()
  593. {
  594.     m_pFont->DeleteDeviceObjects();
  595.     SAFE_RELEASE( m_pVB );
  596.     return S_OK;
  597. }
  598.  
  599.  
  600.  
  601.  
  602. //-----------------------------------------------------------------------------
  603. // Name: FinalCleanup()
  604. // Desc: Called before the app exits, this function gives the app the chance
  605. //       to cleanup after itself.
  606. //-----------------------------------------------------------------------------
  607. HRESULT CMyD3DApplication::FinalCleanup()
  608. {
  609.     SAFE_DELETE( m_pFont );
  610.  
  611.     return S_OK;
  612. }
  613.  
  614.  
  615.  
  616.  
  617. //-----------------------------------------------------------------------------
  618. // Name: ConfirmDevice()
  619. // Desc: Called during device intialization, this code checks the device
  620. //       for some minimum set of capabilities
  621. //-----------------------------------------------------------------------------
  622. HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,
  623.                                           D3DFORMAT Format )
  624. {
  625.     if( pCaps->DevCaps & D3DDEVCAPS_RTPATCHES )
  626.         return S_OK;
  627.     else
  628.         return E_FAIL;
  629. }
  630.  
  631.  
  632.  
  633.  
  634. //-----------------------------------------------------------------------------
  635. // Name: MsgProc()
  636. // Desc: Message proc function to handle key and menu input
  637. //-----------------------------------------------------------------------------
  638. LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
  639.                                     LPARAM lParam )
  640. {
  641.     // Pass mouse messages to the ArcBall so it can build internal matrices
  642.     m_ArcBall.HandleMouseMessages( hWnd, uMsg, wParam, lParam );
  643.  
  644.     if( uMsg == WM_COMMAND)
  645.     {
  646.         switch( LOWORD(wParam) )
  647.         {
  648.         case IDM_MORESEGMENTS:
  649.             m_numSegments++;
  650.             break;
  651.  
  652.         case IDM_LESSSEGMENTS:
  653.             if( m_numSegments > 1 )
  654.             m_numSegments--;
  655.             break;
  656.  
  657.         case IDM_TOGGLEHELP:
  658.             m_bShowHelp = !m_bShowHelp;
  659.             break;
  660.  
  661.         case IDM_TOGGLEWIREFRAME:
  662.             m_bWireframe = !m_bWireframe;
  663.             break;
  664.         }
  665.     }
  666.  
  667.     return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
  668. }
  669.